#pragma once
#include<vector>

namespace yj
{
	template<class T,class container = vector<T>,class Compare = less<T>>
	class priority_queue
	{
	public:
		void adjust_up(size_t _child)
		{
			Compare _com;
			size_t  _parent = (_child - 1) / 2;
			while (_child > 0)
			{
				if (_com(_con[_parent] , _con[_child]))
				{
					swap(_con[_parent], _con[_child]);
					_child = _parent;
					_parent = (_child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjust_down(size_t _parent)
		{
			Compare _com;

			size_t _child = _parent * 2 + 1;
			
			while (_child < _con.size())
			{
				if (_child + 1 < _con.size() && _com(_con[_parent] , _con[_child]))
				{
					_child += 1;
				}

				if(_com(_con[_parent], _con[_child]))
				{
					swap(_con[_parent], _con[_child]);
					_parent = _child;
					_child = _parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(_con.size()-1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjust_down(0);
		}

		const T& top()
		{
			return _con[0];
		}

		bool empty()
		{
			return _con.empty();
		}

	private:
		container _con;
	};
}